/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gipf.trunk;

import gipf.trunk.NonMovable.Node;
import gipf.trunk.Pieces.Piece;
import java.awt.Point;
import java.util.ArrayList;

/**
 *
 * @author Eduardo
 */
public class BoardMech implements Board {

    NonMovable nonMovable;
    Pieces piecesGroup;
    static ArrayList<Piece> boardSnapShot;

    public NonMovable getNodes() {
        return nonMovable;
    }

    public BoardMech() {
    }

    @Override
    public void build() {
        this.nonMovable = new NonMovable(); // only 2 instances for creating
        // these elements
        this.piecesGroup = new Pieces();
    }
       
    @Override
    public void deleteLastPiece(){
        piecesGroup.removeLast();
    }
    
    @Override
    public void dummyFill(Player playerInTurn, Player oponent){
        this.piecesGroup.createPiece(playerInTurn.id_player,
                    this.nonMovable.getVertices().get(0));
    this.piecesGroup.createPiece(playerInTurn.id_player,
                    this.nonMovable.getVertices().get(4));
    this.piecesGroup.createPiece(playerInTurn.id_player,
                    this.nonMovable.getVertices().get(13));
    this.piecesGroup.createPiece(playerInTurn.id_player,
                    this.nonMovable.getVertices().get(28));
    this.piecesGroup.createPiece(oponent.id_player,
                    this.nonMovable.getVertices().get(1));
    this.piecesGroup.createPiece(oponent.id_player,
                    this.nonMovable.getVertices().get(7));
    this.piecesGroup.createPiece(oponent.id_player,
                    this.nonMovable.getVertices().get(19));
    
    }
    
    @Override
    public void takeSnapShot() {
        BoardMech.boardSnapShot = this.piecesGroup.getPiecelist();
    }

    @Override
    public boolean fullLine(int to, int from) {
        int direction = nonMovable.getVertices().get(from).adjacent(nonMovable.getVertices().get(to));
        Node fromNode = nonMovable.getVertices().get(from);
        if (direction == 6) {
            return true;
        }
        return piecesGroup.fullLine(
                fromNode,
                direction);
    }

    @Override
    public void buildReserves(ArrayList<Player> playersList, String setting) {
        for (Player player : playersList) {
            player.setReserves(this.nonMovable.createReserve(setting));
        }
    }

    public boolean fourTogether(){
        ArrayList<ArrayList<Integer>> alignedNodes = new  ArrayList<ArrayList<Integer>>();
        ArrayList<Piece> completedLines = piecesGroup.getLast();
        
        for(Piece piece:completedLines){
         for (int direction = 0; direction < 3; direction++){
                 alignedNodes.add(piecesGroup.wholeLineIndexList(piece, direction));

         }
        }
        boolean fourTogether = false;
        
        int player0Count = 0;
        int player1Count = 0;
 
        ArrayList<ArrayList<Piece>> lists = new ArrayList<ArrayList<Piece>>();
        for(ArrayList<Integer> nodeIndexes : alignedNodes){
            ArrayList<Piece> connected1 = new ArrayList<Piece>();ArrayList<Piece> connected2 = new ArrayList<Piece>();

            for(Integer nodeIndex:nodeIndexes){
            Piece alignedPiece = piecesGroup.onNode(nonMovable.getVertices().get(nodeIndex));
            
            if(alignedPiece == null){
                player0Count = 0; player1Count = 0; connected1.clear(); connected2.clear();
            }else{
                if(alignedPiece.getPlayer() == 0){connected1.add(alignedPiece);player0Count++; player1Count = 0;}
                if(alignedPiece.getPlayer() == 1){connected2.add(alignedPiece);player1Count++; player0Count = 0;}
            }
            if(player0Count >=4){
                lists.add(connected1);
                
                System.out.println("valid line: 0" );
                 fourTogether = true;
            }
            if( player1Count >= 4){
                lists.add(connected2);
                System.out.println("valid line: 1" );
                 fourTogether = true;
            }
         
        
            }
        
        }
        
        
        
        System.out.println("-**************************");
        System.out.println("player0: " + player0Count);
        System.out.println("player1: " + player1Count);
        return fourTogether;
    }
    
    @Override
    public boolean reservesValid(ArrayList<Player> playersList) {
        for (Player player : playersList) {
            if (player.pieceReserves.isReserveEmpty()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void executeMove(Command cmd, Player playerInTurn) {

        if (cmd.extractHead() == "move") {
            Pieces.Piece from;

            
            
            int direction = nonMovable.getVertices().get(cmd.getFrom()).adjacent(nonMovable.getVertices().get(cmd.extractIndex()));

            for (int i = 0; i < piecesGroup.getPiecelist().size(); i++) {
                from = piecesGroup.getPiecelist().get(i);
                if (from.getCurrent().index() == cmd.getFrom()) {
                    piecesGroup.resetLastMoves();
                    piecesGroup.move(from, direction);
                }
                System.out.println(piecesGroup.lineIndexList(from, direction));
                System.out.println(piecesGroup.alignedPieces(from, direction));
            }

//            System.out.println(cmd.extractHead()
//                    + nonMovable.getVertices().get(cmd.getFrom()).adjacent(
//                    nonMovable.getVertices().get(
//                    cmd.extractIndex())));

        } else if (cmd.extractHead() == "place") {
            //System.out.println(cmd.extractHead()
               //     + nonMovable.getVertices().get(cmd.getFrom()).adjacent(
              //      nonMovable.getVertices().get(
              //      cmd.extractIndex())));
            //System.out.println("player:" + playerInTurn.id_player);
            this.piecesGroup.createPiece(playerInTurn.id_player,
                    this.nonMovable.getVertices().get(cmd.extractIndex()));
            this.piecesGroup.getPiecelist().get(piecesGroup.getPiecelist().size() - 1).getCurrent().setPositionPoint(cmd.extractPoint());
        }

    }

    public boolean inner(Command cmd) {
        // if (linesReady)
        int index = cmd.extractIndex();
        for (int i = 0; i < 19; i++) {
            if (index == i) {
                return true;
            }
        }
        return false;
    }

    public boolean second(Command cmd) {
        int index = cmd.extractIndex();
        for (int i = 19; i < 37; i++) {
            if (index == i) {
                return true;
            }
        }
        return false;
    }

    public boolean outter(Command cmd) {
        int index = cmd.extractIndex();
        for (int i = 37; i < 61; i++) {
            if (index == i) {
                return true;
            }
        }
        return false;
    }

    public boolean checkPlacement(Command cmd) {
        boolean isValid = false;

        for (superPoint pointElement : BoardComponent.superPointList) {
            if ((Math.abs(pointElement.getX() - cmd.extractPoint().getX()) <= BoardComponent.tolerance)
                    && (Math.abs(pointElement.getY()
                    - cmd.extractPoint().getY()) <= BoardComponent.tolerance)) {
//                System.out.println("CORRECT place @" + pointElement.getX()
//                        + ", " + pointElement.getY());

                cmd.addIndex(pointElement.getIndex());
//                System.out.println(pointElement.getIndex());

                isValid = true;
                break;
            }

        }

        return isValid;

    }

    
    
    @Override
    public boolean possible(int extractIndex, int from) {
        // TODO Auto-generated method stub
        return true;
    }
    // public int nodeIDlocated(Command cmd){
    // boolean isValid = false;
    //
    // for(Point pointElement: BoardComponent.pointList){
    // if ( (Math.abs(pointElement.x - cmd.extractPoint().x) <=
    // BoardComponent.toletance) && (Math.abs(pointElement.y -
    // cmd.extractPoint().y) <= BoardComponent.toletance) ){
    // isValid = true;
    // break;
    // }
    //
    // }
    //
    // return isValid;
    //
    // }
}
